package com.hspedu.newData.dmsxl.树.二叉树的遍历.递归;

import com.hspedu.newData.dmsxl.ds.TreeNode;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-13 
 * @Version: 1.0
 */
// todo
// 没看明白
public class LC1022从跟到叶的二进制数之和 {

    public int sumRootToLeaf(TreeNode root) {

        return dfs(root,0);

    }

    public int dfs(TreeNode root,int val){
        if (root == null) return 0;

        // 将上一层的值加上这一层的值
        val = (val<<1) | root.val;

        if (root.right == null && root.left == null){
            return val;
        }
        return dfs(root.left,val) + dfs(root.right,val);
    }
}
